﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AreaManager.aspx.cs" Inherits="Card.Manager.AreaManager" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>地区维护</title>
    <link href="Css/Style.css" rel="Stylesheet" />
    <link href="css/ui-lightness/jquery-ui-1.9.2.custom.min.css" rel="Stylesheet" />

    <script type="text/javascript" src="JavaScript/jquery-1.8.3.min.js"></script>

    <script src="JavaScript/jquery-ui-1.9.2.custom.min.js" type="text/javascript"></script>

    <style type="text/css">
        .Tree { font-size: 10pt; }
        .Tree ul { list-style: none; margin-top: 10px; }
        .Tree ul li { padding: 5px; border-left: #a4a4a4 1px solid; }
        .Tree ul li span { height: 40px; margin-left: 10px; cursor: pointer; font-weight: bold; }
        .Tree ul li span a { margin-left: 10px; margin-right: 10px; font-weight: normal; }
        .Tree_Item { background-color: #E9F2F7; }
        .Tree_Alter { background-color: #CADEE8; }
        .Tree .folder_collapsable, .Tree .folder_expandable, .Tree .file { background: url(images/folder.png) no-repeat; padding-left: 25px; line-height: 23px; padding-top: 5px; }
        .Tree .folder_expandable { background-position: 0 0; }
        .Tree .folder_collapsable { background-position: 0 -50px; }
        .Tree .file { background-position: 0 -100px; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="Tree">
        <ul>
            <li class="Tree_Alter"><span onclick="$(this).parent().children('ul').first().toggle();">
                地区列表</span><span><a href="javascript:void(0);addDetail('0','1');">添加地区</a></span>
                <ul>
                    <asp:Repeater ID="rptProvince" runat="server">
                        <ItemTemplate>
                            <li class="Tree_Item" id="li_<%# Eval("ID") %>"><span class="folder_expandable" onclick="loadChildren('<%# Eval("ID") %>')">
                                <%# Eval("NAME") %></span><span>【排序：<%#Eval("Sort") %>】</span><span><a href="javascript:void(0);editDetail('<%# Eval("ParentAreaID") %>','<%# Eval("ID") %>')">编辑</a>
                                    |<a href="javascript:void(0);deleteArea('<%# Eval("ID") %>');">删除</a> |<a href="javascript:void(0);addDetail('<%# Eval("ID") %>','<%# int.Parse(Eval("AreaType").ToString()) + 1 %>');">添加地区</a>
                                </span></li>
                        </ItemTemplate>
                        <AlternatingItemTemplate>
                            <li class="Tree_Alter" id="li_<%# Eval("ID") %>"><span class="folder_expandable"
                                onclick="loadChildren('<%# Eval("ID") %>')">
                                <%# Eval("NAME") %></span><span>【排序：<%#Eval("Sort")%>】</span> <span><a href="javascript:void(0);editDetail('<%# Eval("ParentAreaID") %>','<%# Eval("ID") %>')">
                                    编辑</a> |<a href="javascript:void(0);deleteArea('<%# Eval("ID") %>');">删除</a> |<a
                                        href="javascript:void(0);addDetail('<%# Eval("ID") %>','<%# int.Parse(Eval("AreaType").ToString())+1 %>');">添加地区</a>
                                </span></li>
                        </AlternatingItemTemplate>
                    </asp:Repeater>
                </ul>
            </li>
        </ul>
    </div>
    <div style="display: none">
        <asp:Button ID="btnDelete" Text="删除" runat="server" />
        <asp:TextBox ID="txtAreaID" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

<script type="text/javascript">
var areas = <%=this.ProvinceJson %>;
var currentID;

function loadChildren(parentID)
{        
    if(areas[parentID]){
        showChildren(parentID);           
    }else{
        currentID = parentID;
        $.get("handler.ashx", 
                 {
                      t:"ga",
                      id:parentID
                 },
                function(data){
                    var temp = eval("("+data+")");
                    areas[currentID] = temp[currentID];
                    showChildren(currentID);
         });
    }
}

function showChildren(parentID){
    if(areas[parentID]){
        var parent = $("#li_" + parentID);
        
        if(parent.length > 0 && parent.children("ul").length > 0){
             parent.children("ul").first().toggle();             
        }else{
            var ul = $("<ul></ul>");
            var alterClass = parent.hasClass("Tree_Alter")?"Tree_Alter":"Tree_Item";
            var itemClass = parent.hasClass("Tree_Alter")?"Tree_Item":"Tree_Alter";
            $(areas[parentID]).each(function(i){
                    $("<li id='li_"+this.ID+"' class='"
                    + (i%2 == 0 ? itemClass:alterClass)
                    +"'><span "
                    + (this.AreaType == "3"?"":"class='folder_expandable' onclick=loadChildren('"+this.ID+"')")
                    +">" + this.Name +"</span><span>"
                    +"【排序：" + this.Sort + "】</span>"
                    +(this.AreaType == "3"?"<span>【开通分站："+this.IsActive+"】</span><span>【代码："+this.SiteCode+"】</span><span>【分站网址："+this.SiteUrl+"】</span>":"")
                    +"<span><a href=\"javascript:void(0);editDetail('"+this.ParentAreaID+"','"+this.ID+"');\">编辑</a>"
                    +"|<a href=\"javascript:void(0);deleteArea('"+this.ID+"');\">删除</a>"
                    +(this.AreaType == "3"?"":"|<a href=\"javascript:void(0);addDetail('"+this.ID+"','"+ (parseInt(this.AreaType)+1) +"');\">添加地区</a>")
                    +(this.IsActive=="Y"?"|<a href='SiteManager.aspx?id="+this.ID+"'>管理员</a>|<a href='SiteSetting.aspx?c="+this.SiteCode+"'>配置</a></span>":"</span>")
                    +"</li>").appendTo(ul);
             });
            
            ul.appendTo(parent);
        }
        
        if(parent.children("ul").first().is(":visible")){
            parent.children("span").first().removeClass("folder_expandable");
            parent.children("span").first().addClass("folder_collapsable");
         }else{
            parent.children("span").first().removeClass("folder_collapsable");
            parent.children("span").first().addClass("folder_expandable");
         }
    }
}

function getAranByParent(parentID,ID){
    if(areas[parentID]){
        var area;
        $(areas[parentID]).each(function(i){
            if(this.ID == ID){
                area = this;
                return false;
            }
        });
        
        return area;
    }
}


function getAreaTypeName(areaType){
    var levelName;
    switch(areaType)
    {
        case "1":
            levelName = "省/直辖市";
            break;
        case "2":
            levelName = "市";
            break;
        case "3":
            levelName = "县/区";
            break;
    }
    return levelName;
}

function editDetail(parentID,ID)
{
    window.location="AreaEdit.aspx?iD="+ID;
}

function addDetail(parentID,areaType)
{
    window.location="AreaEdit.aspx?parentId="+parentID;   
}

function deleteArea(ID)
{
    if($("#li_"+ID).children("ul").length > 0){
        alert("该地区存在子地区，不能删除！");
        return;
    }
    
    if(confirm("确认删除地区？")){
        $("#txtAreaID").val(ID);
        $("#btnDelete").click();
    }
}
</script>

